package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 显示供应商信息
     * @param page 当前页
     * @param rows  每页显示的记录数
     * @param supplierName
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows, String supplierName){
        List<Supplier> list = supplierService.selectList(page,rows,supplierName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        map.put("total",list.size());
        return map;
    }

    /**
     * 添加或修改
     * @param supplier
     * @param request
     * @return
     */
    @PostMapping("save")
    public ServiceVO saveOrUpdate(Supplier supplier, HttpServletRequest request){
        String supplierId = request.getParameter("supplierId");
        supplierService.saveOrUpdate(supplier,supplierId);
        return new ServiceVO(100,"请求成功");
    }

    @PostMapping("delete")
    public ServiceVO delete(String ids){
        Boolean flag = supplierService.delete(ids);
        if (flag){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(200,"请求失败");
    }
}
